/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupItemMapper;
import com.itheima.health.dao.CheckItemMapper;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <dl>
 * <dd>描述: ~ CheckItemServiceImpl</dd>
 * <dd>创建时间：  10:23 2021/1/4</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/1/4      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@DubboService
@Slf4j
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;
    @Autowired
    private CheckGroupItemMapper checkGroupItemMapper;

    /**
     * @Author: guodong
     * @Date: 11:12 2021/1/4
     * @Parms [queryPageBean]
     * @ReturnType: com.itheima.health.entity.PageResult
     * @Description: 分页查询
    */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[检查项-分页查询]data：{}", queryPageBean);
        //1.使用分页插件分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.分页查询
        Page<CheckItem> page = checkItemMapper.findPage(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * @Author: guodong
     * @Date: 11:38 2021/1/4
     * @Parms [checkItem]
     * @ReturnType: void
     * @Description: 新增检查项
    */
    @Override
    public void add(CheckItem checkItem) {
        log.info("[检查项-新增]data：{}", checkItem);
        checkItemMapper.insertSelective(checkItem);
    }

    /**
     * @Author: guodong
     * @Date: 12:16 2021/1/4
     * @Parms [id]
     * @ReturnType: void
     * @Description: 根据id删除检查项
    */
    @Override
    public void delete(Integer id) {
        log.info("[检查项-根据id删除]data: {}", id);
        /*
            在删除检查项之前需要判断该检查项是否有被检查组所使用
            如果使用则不可以删除
            需要查询中间关联关系表 t_checkgroup_checkitem 判断是否有引用checkitem
         */
        //1.查询是否有关联检查项
        CheckGroupItem checkGroupItem = new CheckGroupItem();
        checkGroupItem.setCheckitemId(id);
        int count = checkGroupItemMapper.selectCount(checkGroupItem);
        if(count>0){//不能删除
            throw new BusinessException("该检查项有关联数据不可以删除...");
        }
        //2.如果没有关联数据那么可以删除
        checkItemMapper.deleteByPrimaryKey(id);
    }

    /**
     * @Author: guodong
     * @Date: 14:50 2021/1/4
     * @Parms [id]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据id查询
     */
    @Override
    public CheckItem findById(Integer id) {
        log.info("[检查项-根据id查询]data：{}", id);
        return checkItemMapper.selectByPrimaryKey(id);
    }

    /**
     * @Author: guodong
     * @Date: 15:02 2021/1/4
     * @Parms [checkItem]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 编辑检查项
     */
    @Override
    public void edit(CheckItem checkItem) {
        log.info("[检查项-编辑检查项]data: {}", checkItem);
        checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    /**
     * @Author: guodong 
     * @Date: 17:48 2021/1/4
     * @Parms []
     * @ReturnType: java.util.List<com.itheima.health.pojo.CheckItem>
     * @Description: 查询所有
    */
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.selectAll();
    }
}
